﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet :MonoBehaviour, IRuseable
{
    float time=0;
    float flyTimes;
    float speed;
    public static bool isExl;

    public void Onspawn()
    {
        flyTimes = 3;
        isExl = false;
    }

    public void OnUnspawn()
    {
        time = 0;
    }
    public void LoadInit(Vector3 initPos,Vector3 target,float speed)
    {
        transform.position = initPos;
        transform.LookAt(target);
        this.speed = speed;
    }
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update ()
    {
        time += Time.deltaTime;
        if (!IsHitTarget())
        {
            if (isExl == false)
            {
                if (time >= flyTimes)
                {
                    Pools.Instance.Unspawn(this.gameObject);
                }
            }
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }

    }

    //判断是否击中目标
    GameObject monster;
    bool IsHitTarget()
    {
        foreach(var e in Spawner.monsters)
        {
            if(Vector3.Distance(transform.position, e.transform.Find("CATRigPelvis").transform.position)<0.3)
            {
                monster = e;
                break;
            }
        }
        if(monster!=null)
        {
            monster.GetComponent<Monster>().Attacktted();
            monster = null;
            Pools.Instance.Unspawn(this.gameObject);
            return true;
        }
        return false;
    }
}
